/*
 * Copyright 2006-2008 Michele Mostarda ( michele.mostarda@gmail.com ).
 * All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the 'License');
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an 'AS IS' BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.graphnav.aggregator.storage.impl.jenastorage;

import org.graphnav.aggregator.storage.TripleIterator;
import org.graphnav.aggregator.storage.TripleFilter;
import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.rdf.model.Statement;

import java.net.URI;
import java.net.URISyntaxException;

/**
 * Jena Triple iterator.
 */
public class JenaTripleIterator implements TripleIterator {

    private TripleFilter filter;

    private StmtIterator iterator;

    Statement statement = null;

    public JenaTripleIterator(TripleFilter flt, StmtIterator iter) {
        filter   = flt;
        iterator = iter;
    }

    public boolean hasNext() {
        if(filter == null) {
            return iterator.hasNext();
        }

        iterator = null;
        while(iterator.hasNext()) {
            statement = iterator.nextStatement();
            try {
                if(
                        filter.acceptSubject( new URI( statement.getSubject().toString() ) )
                                &&
                        filter.acceptPredicate(new URI(statement.getPredicate().getURI() ) )
                                &&
                        filter.acceptObject( statement.getObject().toString() )
                ) {
                   return true;
                }
            } catch (URISyntaxException e) {
                throw new IllegalStateException();
            }
        }
        return false;
    }

    public void next() {
        if(filter == null) {
            statement = iterator.nextStatement();
        } else {
            if(iterator == null) { throw new IllegalStateException(); }
        }
    }

    public URI getSubject() {
        try {
            return new URI( statement.getSubject().getURI() );
        } catch (URISyntaxException e) {
            throw new IllegalStateException(e);
        }
    }

    public URI getPredicate() {
         try {
            return new URI( statement.getPredicate().getURI() );
        } catch (URISyntaxException e) {
            throw new IllegalStateException(e);
        }
    }

    public URI getObjectAsURI() {
        if( ! statement.getObject().isURIResource()) { throw new IllegalStateException(); }
        try {
            return new URI( statement.getObject().toString() );
        } catch (URISyntaxException e) {
            throw new IllegalStateException(e);
        }
    }

    public String getObjectAsString() {
        return statement.getObject().toString();
    }

    public boolean objectIsURI() {
         return statement.getObject().isURIResource();
    }
}
